﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Entity;

namespace Job.Web
{
    public class MySession
    {
        #region 后台管理员在Seesion中保存内容操作

        //保存在Seesion中的后台管理员关键字
        private string KEY_ADMIN_USER = "admin_user"; 

        //保存后台管理员的信息到Session中
        public void AddAdminUser2Session(JobUser user) 
        {
            if (null == user) return;
            HttpContext.Current.Session.Remove(KEY_ADMIN_USER);
            HttpContext.Current.Session.Add(KEY_ADMIN_USER,user);

            //如果是后台管理员代替普通的企业操作职位等资料，
            //需要要Seesion中加一个CompanyID为0的Company的Seesion
            SessionCompany company = new SessionCompany();
            company.CompanyId = 0;
            company.CompanyName = "";
            company.LoginCode = user.LoginName;
            AddCompany2Session(company);

            //如果是后台管理员代替普通的个人查询职位等资料，
            //需要要Seesion中加一个PersonID为0的Person的Seesion
            SessionPersonal person = new SessionPersonal();
            person.PersonAccountid = 0;
            person.LoginName = user.LoginName;
            person.UserName = "";
            AddPersonal2Session(person);
        }
        
        //获得当前登录的后台管理员信息
        public JobUser GetAdminUserFromSession()
        {
            string key;
            for( int i = 0 ; i < HttpContext.Current.Session.Keys.Count ; i++ )
            {
                key = (string)HttpContext.Current.Session.Keys[i];
                if (key.Equals(KEY_ADMIN_USER))
                {
                    return (JobUser)HttpContext.Current.Session[KEY_ADMIN_USER];
                }
            }
            return null;
        }
        //将后台管理员的信息从Session中删除
        public void RemoveAdminUserFromSession()
        {
            try
            {
                HttpContext.Current.Session.Remove(KEY_ADMIN_USER);
                HttpContext.Current.Session.Remove(KEY_COMPANY);
            }
            catch (Exception ex)
            {
            }
        }

        #endregion

        #region Company在Seesion中保存内容操作

        //保存在Seesion中的企业关键字
        private string KEY_COMPANY = "company_user";

        //保存企业的信息到Session中
        public void AddCompany2Session(SessionCompany company)
        {
            //为了支持后台可以代替企业操作
            if (company.LoginCode == null) company.LoginCode = "";
            company.LoginCode = company.LoginCode.Trim();

            if (null == company) return;
            HttpContext.Current.Session.Remove(KEY_COMPANY);
            HttpContext.Current.Session.Add(KEY_COMPANY, company);
        }

        //获得当前登录的企业信息
        public SessionCompany GetCompanyFromSession()
        {
            string key;
            for (int i = 0; i < HttpContext.Current.Session.Keys.Count; i++)
            {
                key = (string)HttpContext.Current.Session.Keys[i];
                if (key.Equals(KEY_COMPANY))
                {
                    return (SessionCompany)HttpContext.Current.Session[KEY_COMPANY];
                }
            }
            return null;
        }
        //将Company的信息从Session中删除
        public void RemoveCompanyFromSession()
        {
            HttpContext.Current.Session.Remove(KEY_COMPANY);
        }

        #endregion

        #region 个人信息在Seesion中保存内容操作

        //保存在Seesion中的个人关键字
        private string KEY_PERSONAL= "personal_user";

        //保存个人的信息到Session中
        public void AddPersonal2Session(SessionPersonal personal)
        {
            if (null == personal) return;
            HttpContext.Current.Session.Remove(KEY_PERSONAL);
            HttpContext.Current.Session.Add(KEY_PERSONAL, personal);
        }

        //获得当前登录的企业信息
        public SessionPersonal GetPersonalFromSession()
        {
            string key;
            for (int i = 0; i < HttpContext.Current.Session.Keys.Count; i++)
            {
                key = (string)HttpContext.Current.Session.Keys[i];
                if (key.Equals(KEY_PERSONAL))
                {
                    return (SessionPersonal)HttpContext.Current.Session[KEY_PERSONAL];
                }
            }
            return null;
        }
        //将Company的信息从Session中删除
        public void RemovePersonalFromSession()
        {
            HttpContext.Current.Session.Remove(KEY_PERSONAL);
        }

        #endregion

    }
}
